package com.agritech.machine;

/**
 * 农业机械抽象基类
 * 定义了所有农业机械共有的属性和行为
 */
public abstract class AgriculturalMachine {
    private String id; // 农机唯一标识
    private String type; // 农机类型 (例如：拖拉机、播种机)
    private String status; // 当前状态 (例如：空闲、工作中、维修中)
    private double fuelLevel; // 燃油水平 (%)
    private double currentLatitude; // 当前纬度
    private double currentLongitude; // 当前经度
    private boolean isRunning; // 是否正在运行
    private AgriculturalMachineContext stateContext; // 状态模式上下文

    public AgriculturalMachine(String id, String type) {
        this.id = id;
        this.type = type;
        this.status = "初始化"; // 初始状态
        this.fuelLevel = 100.0; // 初始满油
        this.currentLatitude = 0.0; // 默认初始位置
        this.currentLongitude = 0.0;
        this.isRunning = false;
        this.stateContext = new AgriculturalMachineContext(this); // 初始化状态上下文
        System.out.println("创建农机: " + type + " (ID: " + id + ")，初始燃油: " + fuelLevel + "%");
    }

    // 抽象方法，由具体农机类实现
    // public abstract void start(); // 由状态模式接管
    // public abstract void stop();  // 由状态模式接管
    // public abstract void performTask(String taskDetails); // 由状态模式接管

    // 状态模式接管的方法
    public void start() {
        stateContext.start();
    }

    public void stop() {
        stateContext.stop();
    }

    public void performTask(String taskDetails) {
        stateContext.performTask(taskDetails);
    }

    public void repair() {
        stateContext.repair();
    }

    /**
     * 移动到指定位置
     * @param latitude 目标纬度
     * @param longitude 目标经度
     */
    public void moveTo(double latitude, double longitude) {
        System.out.println(type + " " + id + " 正在移动到位置: (" + latitude + ", " + longitude + ")");
        this.currentLatitude = latitude;
        this.currentLongitude = longitude;
        consumeFuel(1.0); // 移动消耗少量燃油
    }

    /**
     * 消耗燃油
     * @param amount 消耗量 (%)
     */
    public void consumeFuel(double amount) {
        this.fuelLevel -= amount;
        if (this.fuelLevel < 0) {
            this.fuelLevel = 0;
        }
        System.out.println(type + " " + id + " 消耗燃油 " + amount + "%，剩余燃油: " + String.format("%.1f", fuelLevel) + "%");
        if (this.fuelLevel == 0 && isRunning) {
            System.out.println("警告: " + type + " " + id + " 燃油耗尽！自动停止。");
            stop(); // 燃油耗尽自动停止
            // 可以在这里触发低燃油异常事件，或通过状态模式处理
            if (!(stateContext.getCurrentState() instanceof com.agritech.state.BrokenState)) {
                 stateContext.setState(new com.agritech.state.BrokenState("燃油耗尽"));
            }
        }
    }

    /**
     * 加油
     * @param amount 加油量 (%)
     */
    public void refuel(double amount) {
        this.fuelLevel += amount;
        if (this.fuelLevel > 100.0) {
            this.fuelLevel = 100.0;
        }
        System.out.println(type + " " + id + " 加油 " + amount + "%，当前燃油: " + String.format("%.1f", fuelLevel) + "%");
    }

    /**
     * 更新农机状态描述
     * @param newStatus 新的状态描述
     */
    public void updateStatus(String newStatus) {
        this.status = newStatus;
        System.out.println(type + " " + id + " 状态更新为: " + status);
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public String getType() {
        return type;
    }

    public String getStatus() {
        // 优先从状态上下文获取精确状态名
        if (stateContext != null && stateContext.getCurrentState() != null) {
            return stateContext.getCurrentState().getStateName();
        }
        return status; 
    }

    public double getFuelLevel() {
        return fuelLevel;
    }
    
    public void setFuelLevel(double fuelLevel) {
        this.fuelLevel = Math.max(0, Math.min(100, fuelLevel));
    }

    public double getCurrentLatitude() {
        return currentLatitude;
    }

    public double getCurrentLongitude() {
        return currentLongitude;
    }

    public boolean isRunning() {
        return isRunning;
    }

    public void setRunning(boolean running) {
        isRunning = running;
    }
    
    public AgriculturalMachineContext getStateContext() {
        return stateContext;
    }
}